package org.kl.wx.service;

import java.util.List;
import java.util.Map;

import me.chanjar.weixin.mp.bean.WxMpGroup;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.kl.wx.entity.App;
import org.kl.wx.entity.Groups;
import org.kl.wx.entity.WxUser;
import org.kl.wx.repository.GroupsDao;
import org.kl.wx.repository.WxUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;

@Component
@Transactional
public class GroupsService {
	private GroupsDao groupsDao;
	private WxUserDao wxUserDao;

	public Groups getGroups(String guid) {
		return groupsDao.findOne(guid);
	}

	public void saveGroups(Groups entity) {
		groupsDao.save(entity);
	}

	public void deleteGroups(String id) {
		groupsDao.delete(id);
	}

	public Groups findByAppIdAndOriginGroupId(String appId, Integer originGroupId) {
		return groupsDao.findByAppIdAndOriginGroupId(appId, originGroupId);
	}

	/**
	 * 删除多条记录
	 * 
	 * @param ids
	 */
	/*
	 * public String multiDelete(String ids) { String[] idArray =
	 * StringUtils.split(ids, ","); int i = 0; for (String id : idArray) { if
	 * (StringUtils.isNotBlank(id)) { groupsDao.delete(id); i++; } } return "删除"
	 * + i + "条记录"; }
	 */

	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0, j = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				List<WxUser> WList = wxUserDao.findByGroupsId(id);
				j += WList.size();
				wxUserDao.delete(WList);
				groupsDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录,并且删除" + j + "条用户信息";
	}

	/**
	 * 通用查改增删方法
	 * 
	 * @param effectRow
	 */
	public void crud(String inserted, String updated, String deleted) {
		// “新增”和“修改”时检查项目是否唯一
		List<Groups> inserts = JSON.parseArray(inserted, Groups.class);
		if (Collections3.isNotEmpty(inserts)) {
			for (Groups a : inserts) {
				groupsDao.save(a);

			}
		}
		List<Groups> updates = JSON.parseArray(updated, Groups.class);
		if (updates.size() > 0) {
			for (Groups b : updates) {
				groupsDao.save(b);
			}
		}
		List<Groups> deletes = JSON.parseArray(deleted, Groups.class);
		if (Collections3.isNotEmpty(deletes)) {
			groupsDao.deleteInBatch(deletes);
		}
	}

	/**
	 * 同步微信分组
	 * 
	 * @param grouplist
	 */
	public void createByGroupList(List<WxMpGroup> groupList, App app) {
		List<Groups> groups = groupsDao.findByAppId(app.getId());
		int n = 0;
		for (WxMpGroup g : groupList) {
			for (Groups m : groups) {
				if (StringUtils.equals(m.getGroupName(), g.getName())) {
					m.setApp(app);
					m.setOriginGroupId((int) g.getId());
					m.setGroupName(g.getName());
					m.setUserCount((int) g.getCount());
					groupsDao.save(m);
					n = 1;
				}
			}
			if (n == 0) {
				Groups group = new Groups();
				group.setApp(app);
				group.setOriginGroupId((int) g.getId());
				group.setGroupName(g.getName());
				group.setUserCount((int) g.getCount());
				groupsDao.save(group);
			}
			n = 0;
		}
	}

	/**
	 * 按条件查询
	 */
	public List<Groups> getAllGroups() {
		return groupsDao.findAll();
	}

	/**
	 * 按条件查询
	 */
	public List<Groups> getGroups(Map<String, Object> searchParams) {
		Specification<Groups> spec = buildSpecification(searchParams);
		List<Groups> result = groupsDao.findAll(spec);
		return result;
	}

	public List<Groups> getAllGroups(Map<String, Object> searchParams, String sortType, String orderType) {
		Specification<Groups> spec = KLDynamicSpecifications.buildSpecification(searchParams, Groups.class);
		return groupsDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}

	/**
	 * 按条件分页查询
	 */
	public Page<Groups> getUserGroups(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<Groups> spec = buildSpecification(searchParams);

		return groupsDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Groups> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<Groups> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), Groups.class);
		return spec;
	}

	@Autowired
	public void setGroupsDao(GroupsDao groupsDao) {
		this.groupsDao = groupsDao;
	}

	@Autowired
	public void setWxUserDao(WxUserDao wxUserDao) {
		this.wxUserDao = wxUserDao;
	}

}
